<template>
  <div id="mainone" style="width: 100%; height: 550px"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props: ["myStart", "myEnd", "myType"],
  data() {
    return {
      start: this.myStart,
      end: this.myEnd,
      tabledata: [],
      maintype: [],
      type: this.myType,
    };
  },
  // created() {

  // },
  mounted() {
    if (this.type == 1) {
      console.log("draw benkesheng");
    } else if (this.type == 2) {
      console.log("draw yanjiusheng");
    }
    if (!this.start || !this.end) {
      this.axios({
        url: "grad/yearList",
        methods: "get",
      }).then((res) => {
        let years = res.data.data;

        years = years.sort(function (a, b) {
          return a - b;
        });

        //默认为最近一年
        this.start = years[years.length - 1];
        this.end = years[years.length - 1];
        this.get_direction(this.start, this.end);
      });
    } else {
      this.get_direction(this.start, this.end);
    }
  },
  watch: {
    myType(val) {
      this.type = val;
    },
  },
  methods: {
    count_kind(data) {
      console.log("table2:",data);
      let com_nation = 0;
      let com_personal = 0;
      let com_three = 0;
      let arm = 0;
      let party = 0;
      let bus_high = 0;
      let bus_tech = 0;
      let bus_mid = 0;
      let bus_other = 0;
      let other = 0;
      let sum = 0;
      for (let i in data) {
        let cur = data[i]["term"];
        let num = data[i]["num"];
        if (cur == "国有企业") {
          com_nation += num;
        } else if (cur == "民营企业") {
          com_personal += num;
        } else if (cur == "三资企业") {
          com_three += num;
        } else if (cur == "部队") {
          arm += num;
        } else if (cur == "党政机关") {
          party += num;
        } else if (cur == "高等教育单位") {
          bus_high += num;
        } else if (cur == "科研设计单位") {
          bus_tech += num;
        } else if (cur == "中初教育单位") {
          bus_mid += num;
        } else if (cur == "其他事业单位") {
          bus_other += num;
        } else {
          other += num;
        }
        sum += num;
      }
      // 针对大类进行计算
      let company = com_nation + com_personal + com_three;
      let bussiness = bus_high + bus_tech + bus_mid + bus_other;

      // 按照需求排序 big_term term num
      return [
        sum,
        party,
        bussiness,
        company,
        arm,
        other,
        bus_high,
        bus_tech,
        bus_mid,
        bus_other,
        com_nation,
        com_personal,
        com_three,
      ];
    },
    get_dict(temp) {
      let title = [
        "党政机关",
        "事业单位",
        "企业单位",
        "部队",
        "高等教育单位",
        "科研设计单位",
        "中初教育单位",
        "其他事业单位",
        "国有企业",
        "民营企业",
        "三资企业",
      ];
      // 组织数据
      let table = [];
      for (let i = 0; i < 9; i++) {
        let temp2 = {
          aim: "1",
          item: "1",
          num: "1",
        };
        table[i] = temp2;
      }

      // 党政机关
      table[0].aim = title[0];
      table[0].item = title[0];
      table[0].num = temp[1];
      // 事业单位
      for (let i = 1; i < 5; i++) {
        table[i].aim = title[1];
        table[i].item = title[i + 3];
        table[i].num = temp[i + 5];
      }
      // 企业单位
      for (let i = 5; i < 8; i++) {
        table[i].aim = title[2];
        table[i].item = title[i + 3];
        table[i].num = temp[i + 5];
      }
      // 部队
      table[8].aim = title[3];
      table[8].item = title[3];
      table[8].num = temp[4];
      return table;
    },
    fune_tine(table) {
      // 全初始化为0
      let res = [];
      for (let i in table) {
        let temp = {};
        temp.name = table[i].item;
        temp.value = table[i].num;
        res.push(temp);
      }
      return res;
    },
    funetine_legend(table) {
      let party = 0;
      let army = 0;
      let business = 0;
      let company = 0;
      for (let i in table) {
        let num = table[i].num;
        if (table[i].aim == "党政机关") {
          party = party + num;
        } else if (table[i].aim == "企业单位") {
          company = company + num;
        } else if (table[i].aim == "事业单位") {
          business = business + num;
        } else {
          army = army + num;
        }
      }
      // 组合成最终的结果
      let res = [];
      let legend = ["党政机关", "企业单位", "事业单位", "部队"];
      for (let i in legend) {
        let temp = {};
        temp.name = legend[i];
        if (legend[i] == "党政机关") {
          temp.value = party;
        } else if (legend[i] == "企业单位") {
          temp.value = company;
        } else if (legend[i] == "事业单位") {
          temp.value = business;
        } else {
          temp.value = army;
        }
        res.push(temp);
      }
      return res;
    },
    get_direction(start, end) {
      this.axios({
        url: "grad/org_kind",
        method: "get",
        params: {
          start: start,
          end: end,
        },
        headers: {
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
        },
      })
        .then((res) => {
          if (this.type == 1) {
            //本科生
            var temp_data = res.data.data.bachelor;
          } else {
            //研究生
            var temp_data = res.data.data.master;
          }
          let temp = this.count_kind(temp_data);
          let table = this.get_dict(temp);
          res = this.fune_tine(table);
          this.tabledata = res;
          let legend = this.funetine_legend(table);
          this.maintype = legend;
          this.drawChart();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    drawChart() {
      this.myChart = echarts.init(document.getElementById("mainone"));
      let that = this;
      this.myChart.setOption({
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)",
        },
        legend: {
          data: [
            "党政机关",
            "高等教育单位",
            "科研设计单位",
            "中初教育单位",
            "其他事业单位",
            "国有企业",
            "民营企业",
            "三资企业",
            "部队",
          ],
        },
        series: [
          {
            name: "毕业单位性质",
            type: "pie",
            selectedMode: "single",
            center: ["50%", "70%"],
            radius: [0, "30%"],
            label: {
              position: "inner",
              fontSize: 14,
            },
            labelLine: {
              show: false,
            },
            data: that.maintype,
          },
          {
            name: "毕业单位性质",
            type: "pie",
            center: ["50%", "70%"],
            radius: ["45%", "60%"],
            labelLine: {
              length: 30,
            },
            label: {
              formatter: "{a|{a}}{abg|}\n{hr|}\n  {b|{b}：}{c}  {per|{d}%}  ",
              backgroundColor: "#F6F8FC",
              borderColor: "#8C8D8E",
              borderWidth: 1,
              borderRadius: 4,
              rich: {
                a: {
                  color: "#6E7079",
                  lineHeight: 15,
                  align: "center",
                },
                hr: {
                  borderColor: "#8C8D8E",
                  width: "100%",
                  borderWidth: 1,
                  height: 0,
                },
                b: {
                  color: "#4C5058",
                  fontSize: 14,
                  fontWeight: "bold",
                  lineHeight: 25,
                },
                per: {
                  color: "#fff",
                  backgroundColor: "#4C5058",
                  padding: [3, 4],
                  borderRadius: 4,
                },
              },
            },
            data: that.tabledata,
          },
        ],
      });
      window.onresize = () => {
        this.myChart.resize();
      };
    },
  },
};
</script>

<style scope></style>
